package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ViewStoreStateQueryCmd;
import com.jackrain.nea.fc.cp.mapper.ViewStoreEmpMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 店仓状态查询
 *
 * @author xuxinshuai
 * @create 2018/01/26
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "cp")
public class ViewStoreStateQueryCmdImpl extends CommandAdapter implements ViewStoreStateQueryCmd {


    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        ViewStoreEmpMapper viewStoreEmpMapper = ApplicationContextHandle.getBean(ViewStoreEmpMapper.class);

        //查询分组
        List<HashMap> orgmapList = new ArrayList<>();
        orgmapList = viewStoreEmpMapper.selectOrgCountState();
        JSONArray orgArray = listToJsonarray(orgmapList);

        //查询详情
        List<HashMap> mapList = new ArrayList<>();
        mapList = viewStoreEmpMapper.selectStoreState();
        JSONArray storeArray = listToJsonarray(mapList);

        JSONArray dataArray = new JSONArray();

        log.debug("分组详情" + orgArray);
        log.debug("详情" + storeArray);

        for (int a = 0; a < orgArray.size(); a++) {

            JSONObject orgJo = new JSONObject();
            JSONArray detailArray = new JSONArray();
            JSONObject cOrgJo = orgArray.getJSONObject(a);
            String corgename = cOrgJo.getString("orgename");
            String enamecount = cOrgJo.getString("enamecount");

            for (int b = 0; b < storeArray.size(); b++) {

                JSONObject storeJo = storeArray.getJSONObject(b);
                String orgename = storeJo.getString("orgename");
                if (corgename.equals(orgename)) {
                    orgJo.put("orgename", corgename);
                    orgJo.put("count", enamecount);
                    detailArray.add(storeJo);
                    orgJo.put("detail", detailArray);
                }
            }

            dataArray.add(orgJo);

        }

        vh.put("data", dataArray);
        return vh;

    }

    /**
     * list转jsonarray
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {

        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }
}
